package com.yan.springcloud.controller;

import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yan.springcloud.pojo.Dept;
import com.yan.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author ：Yan Guang
 * @date ：Created in 2020/11/28 17:18
 * @description：
 */
@RestController
public class DeptController {

   @Autowired
    private DeptService deptService;


    @GetMapping("/dept/get/queryid")
    @HystrixCommand(fallbackMethod = "hystrixGet")//熔断调用方法
   public Dept get(@PathParam("id") Long id){
       Dept dept = deptService.queryById(id);

       if(dept==null){
           throw new RuntimeException("id=>"+id+"不存在该用户，信息无法找到");
       }

       return  dept;
   }

   //熔断版本,请求失败很多次就会使用该方法
   public Dept hystrixGet(@PathParam("id") Long id){
       Dept dept = new Dept();
       dept.setDeptno(id);
       dept.setDname("id=>"+id+"不存在该用户，信息无法找到");
       dept.setDb_sourse("数据库不存在!");
       return dept;
   }

}
